<?php
declare (strict_types=1);

namespace app\store\controller\student;

use app\store\controller\Controller;
use app\store\model\store\User as StoreUserModel;
use app\store\model\student\Sign as StudentSignModel;
use think\db\exception\DbException;
use think\Model;

class Sign extends Controller
{
    public function details($apiId)
    {
        $details = StudentSignModel::detail($apiId, ['student_user', 'job']);
        return $this->renderSuccess(compact('details'));
    }

    /**
     * 列表
     * @return array
     * @throws DbException
     */
    public function list()
    {
        $model = new StudentSignModel;
        $param = $this->request->param() ?: [];
        $studentSignmodel = new StoreUserModel;
        $param['store_user_id'] = $studentSignmodel->getChildrenUserIds(!empty($param['channel_id']) ? $param['channel_id'] : $this->store['user']['store_user_id']);
        $list = $model->getList($param);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 更新学员
     * @param int $apiId
     * @return array
     */
    public function edit(int $apiId)
    {
        // 管理员详情
        $model = StudentSignModel::detail($apiId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    public function generateCertificate(int $apiId)
    {
        // 管理员详情
        $model = StudentSignModel::detail($apiId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 新增学员
     * @return array
     */
    public function add()
    {
        // 管理员详情
        $model = new StudentSignModel;
        // 更新记录
        if ($student_sign_id = $model->add($this->postForm(),$this->store['user'])) {
            return $this->renderSuccess(['student_sign_id' => $student_sign_id], '添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    public function importExcelFile($file_id)
    {
        // 管理员详情
        $model = new StudentSignModel;
        // 更新记录
        if ($count=$model->importExcelFile($file_id, $this->store['user'])) {
            return $this->renderSuccess('导入成功，本次成功导入'.$count.'条记录！');
        }
        return $this->renderError($model->getError() ?: '导入失败');
    }

    /**
     * 删除学员
     * @param int $apiId
     * @return array|bool
     */
    public function delete(int $apiId)
    {
        // 管理员详情
        $model = StudentSignModel::detail($apiId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    public function reported($apiId)
    {
        $model = new StudentSignModel;
        if (!$model->reported($apiId)) {
            return $this->renderError($model->getError() ?: '上报失败');
        }
        return $this->renderSuccess('上报成功');
    }

    public function generateClassHours($apiId)
    {
        $model = new StudentSignModel;
        if (!$model->generateClassHours($apiId)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
}
